<template>
<!--component/subscribeMessage2/subscribeMessage2.wxml-->
<view class="other-city" @tap="subscribe"><slot></slot></view>
</template>

<script>
const app = getApp();
import util from "../../utils/util";

export default {
  data() {
    return {};
  },

  components: {},
  props: {
    action: {
      default: "",
      type: String
    },
    city: {
      default: "",
      type: String
    },
    text: {
      default: "订阅",
      type: String
    },
    name: {
      default: "",
      type: String
    },
    tepId: {
      default: "",
      type: String
    },
    code: {
      default: "",
      type: String
    }
  },
  methods: {
    // 订阅消息
    subscribe() {
      if (this.name === "名单查询结果" && this.code.length != 13) {
        uni.showToast({
          title: '请修改正确的13位申请编码',
          icon: "none"
        });
        return;
      }

      var tepId = this.tepId;
      if (this.text == "已订阅") return;

      if (uni.getStorageSync("openid")) {
        uni.requestSubscribeMessage({
          tmplIds: [tepId],
          success: res => {
            uni.getSetting({
              withSubscriptions: true,
              success: res => {},
              fail: () => {},
              complete: () => {}
            });

            if (res[tepId] === "accept") {
              let data = {
                action: "msg",
                openid: uni.getStorageSync("openid"),
                city: this.city,
                name: util.getName(this.properties.action) ? util.getName(this.properties.action) : this.name,
                code: this.code ? this.code : ''
              };
              util.request(`sub_message.php`, data, "POST").then(res => {
                uni.showToast({
                  title: '订阅消息成功',
                  icon: 'none'
                });
                this.$emit("subsuccess"); // 统一上报函数

                util.reportMessage("dingyueliang", {
                  action: this.name
                });
              });
            }
          }
        });
      }
    }

  }
};
</script>
<style>
/* component/subscribeMessage2/subscribeMessage2.wxss */
</style>